package view;

import lib.gui.ScreenTemplate;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class SplashScreen extends ScreenTemplate 
{
    public SplashScreen() {
        super("loading ... ", true);
       
        EncodedImage bkg = EncodedImage.getEncodedImageResource("img/splash.png");
        BackgroundFactory.createBitmapBackground(bkg.getBitmap());
        Bitmap b = bkg.getBitmap();
        int srcLeft = (((b.getWidth() - Display.getWidth())/2)+15);
        int srcTop = (b.getHeight() - Display.getHeight()) /2;
        Bitmap scaledBitmap = new Bitmap(Display.getWidth(), Display.getHeight());
        b.scaleInto(srcLeft, srcTop, b.getWidth(), b.getHeight(), scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), Bitmap.FILTER_BOX);
        setBackground(BackgroundFactory.createBitmapBackground(b));
        VerticalFieldManager _bkg = new VerticalFieldManager(Field.USE_ALL_WIDTH)
        {
			private int height = Display.getHeight();
		    protected void sublayout(int maxWidth, int maxHeight) {
		        super.sublayout(maxWidth, height);
		        setExtent(maxWidth, height);
		    }
		};
        _bkg.setBackground(BackgroundFactory.createBitmapBackground(scaledBitmap));
        add(_bkg);
        final Screen me = this;
        new Thread(){
            public void run() {
                // do something that takes a long time
                try { Thread.sleep(5000);} catch (Exception e) {}

                synchronized (UiApplication.getEventLock()) {
                    Mingle next = new Mingle(); // replace with your next screen here
                    //switchScreen(next);
                    UiApplication.getUiApplication().pushScreen(next);
                    UiApplication.getUiApplication().popScreen(me);
                    //System.exit(0);
                }
            }
        }.start();
    }
}